package com.blog.controller.index;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.alibaba.fastjson.JSONObject;
import com.blog.pojo.Blog;
import com.blog.pojo.BlogType;
import com.blog.pojo.Blogger;
import com.blog.pojo.PageBean;
import com.blog.service.BlogService;
import com.blog.service.BlogTypeService;
import com.blog.service.BloggerService;
import com.blog.util.UtilPage;
import com.blog.util.UtilResponse;
import com.blog.util.UtilString;

/**
 * 首页初始化
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/")
public class IndexController {
	@Resource
	private BlogService blogService;
	@Resource
	private BloggerService bloggerService;
	@Resource
	private BlogTypeService blogTypeService;
	
	private int rows = 10;//每页条数
	/**
	 * 主页博客记录分页数据
	 * @param page
	 * @param typeId
	 * @param releaseDateStr
	 * @param request
	 * @return
	 */
	@RequestMapping("index")
	public ModelAndView index(String page,String typeId,String releaseDateStr,HttpServletRequest request ){
		ModelAndView mav = new ModelAndView();
		//判断是否为第一页
		if(UtilString.isEmpty(page) || page == "0" ){
			page = "1";
		}
		//分页bean 
		PageBean bean = new PageBean(Integer.parseInt(page), rows);
		//查询条件
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", bean.getStart());
		map.put("size", bean.getPageSize());
		map.put("typeId", typeId);
		map.put("releaseDateStr", releaseDateStr);
		//装载数据
		List<Blog> blogList = blogService.list(map);
		mav.addObject("blogList",blogList);
		/**
		 * 以下为分页导航参数
		 * 生成分页jsp代码
		 */
		StringBuffer param = new StringBuffer();
		//判断是否有博客分类参数
		if(UtilString.isNotEmpty(typeId)){
			param.append("typeId="+typeId+"&");
		}
		//判断是否有时间参数
		if(UtilString.isNotEmpty(releaseDateStr)){
			param.append("releaseDateStr="+releaseDateStr+"&");
		}
		//装载分页导航
		mav.addObject("pageCode", 
			UtilPage.genPagination(request.getContextPath()+"/index.html", 
					blogService.getTotal(map), Integer.parseInt(page), rows, param.toString()));
		//加载数据页面
		mav.addObject("mainPage","foreground/blog/list.jsp");
		mav.addObject("title","java个人博客系统");
		//数据页面
		mav.setViewName("mainTemp");
		
		return mav;
	} 
	
}
